(function(){
    angular.module('app').controller("agbUserCtrl",function($scope,proxy){
        $scope.permission = PERMISSION;
        $scope.filter = {
            orgID: orgID
        }
        $scope.user = {
            orgID: orgID
        }
        $scope.showUserModal = function(item) {
            item = item || {};
            var user = angular.copy(item);
            $scope.modalTitle = item.id? "新建用户": "编辑用户";
            user.roleName = "请绑定角色";
            if(user.roleID) {
                $scope.roleList.forEach(function(v){
                    if(v.id == user.roleID) {
                        user.roleID = v.id;
                        user.roleName = v.name;
                        return false;
                    }
                })
            }
            $scope.user = user;
            $("#userModal").modal();
        }

        $scope.search = function(value) {
            $scope.filter.name = value;
            getList();
        }

        // 开启/关闭广告主
        $scope.setStatus = function (item) {
            var user = {
                id: item.id,
                state: item.open?"10":"30"
            }
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/pri/user/modify',
                load:true,
                data: {
                    data:user
                },
                success:function(){
                    item.state = item.state == "10"?"20":"10";
                },
                error: function () {
                    // 失败时,将switch状态重置
                    item.open = !item.open
                }
            })
        }

        // 更新user
        $scope.updateUser = function() {
            var user = $scope.user;
            if(!user.loginName) return layer.msg("请输入登录名");
            if(!user.password) return layer.msg("请输入密码");
            if(!user.name) return layer.msg("请输入用户名");
            if(!user.telephone) return layer.msg("请输入手机号");
            if(!user.roleID) return layer.msg("请绑定角色");

            // id存在调用编辑 不存在调用添加
            var url = user.id?STATIC_URL.domian+'/dsp/pri/user/modify':STATIC_URL.domian+'/dsp/pri/user/add';

            proxy.proxy({
                url: url,
                load: true,
                data: {
                    data:user
                },
                success: function(res){
                    layer.msg("操作成功");
                    $("#userModal").modal('hide');
                    getList();
                }
            })
        }

        // 选择角色
        $scope.selectRole = function(item) {
            $scope.user.roleID = item.id;
        }

        // 删除账号
        $scope.deleteUser = function(id) {
            layer.confirm('是否确定删除？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                proxy.proxy({
                    url: STATIC_URL.domian+'/dsp/agency/finance/transfer',
                    load: true,
                    data: {
                        id: id
                    },
                    success: function(){
                        layer.closeAll();
                        layer.msg("删除成功")
                        getList();
                    }
                })
            });
        }

        getRoleList();
        getList()

        // 获得列表
        function getList(current, pagesize) {
            var data = {
                'page': (current || $("#pageToolbar .focus").html() || 1)+"",
                'size': (pagesize || 20)+"",
                'filter': $scope.filter,
                "sort": ["cTime,1"],
                sTime : $scope.sTime,
                eTime : $scope.eTime
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/pri/user/list',
                load: true,
                data: data,
                success: function(res){
                    // 获得列表
                    $scope.list = res.data.data;
                    $("#pageToolbar").html("");
                    $('#pageToolbar').Paging({
                        pagesize: pagesize || '20',
                        current: current || '1',
                        count:res.data.total,
                        toolbar:true,
                        'hash': false,
                        callback:getList
                    });
                }
            })
        }

        // 获得角色列表
        function getRoleList() {
            var data = {
                'page': "1",
                'size': "99999",
                'filter': {
                    roleType: '0'
                },
                "sort": ["cTime,1"],
                sTime : $scope.sTime,
                eTime : $scope.eTime
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/pri/role/list',
                load: true,
                data: data,
                success: function(res){
                    // 获得列表
                    var arr  = res.data.data;
                    // 移除直客角色
                    for(var i = 0; i < arr.length; i++) { 
                        if(arr[i].name == '代理商管理员(直客)') {
                            arr.splice(i,1)
                            break;
                        }
                    }
                    $scope.roleList = arr;
                }
            })
        }
    })
})();